package com.ginage.gaugemanager.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ginage.gaugemanager.mapper.entity.Gauge;
import com.ginage.gaugemanager.service.api.IGaugeService;

@Controller
public class GaugeController {
	@Autowired
	IGaugeService service;

	/*
	 * @RequestMapping("getThList.do")
	 * 
	 * public void getThList() { List<GaugeType> gaugeTypeList = cs.getGaugeType();
	 * List<Department> departmentList = cs.getDepartment(); //String[]
	 * gaugeSpecification = service.getGaugeSpecification(); String[] gaugeAccuracy
	 * = service.getGaugeAccuracy(); String[] gaugeModel = service.getGaugeModel();
	 * String[] gaugeManufacturer = service.getGaugeManufacturer(); String[]
	 * gaugeOwner = service.getGaugeOwner(); String[] gaugeCalibratePeriod =
	 * service.getGaugeCalibratePeriod(); String[] gaugeFolder =
	 * service.getGaugeFolder(); JSONArray ja = new JSONArray(); JSONObject jb1 =
	 * new JSONObject(); JSONObject jb2 = new JSONObject(); JSONObject jb3 = new
	 * JSONObject(); JSONObject jb4 = new JSONObject(); JSONObject jb5 = new
	 * JSONObject(); JSONObject jb6 = new JSONObject(); JSONObject jb7 = new
	 * JSONObject(); JSONObject jb8 = new JSONObject(); JSONObject jb9 = new
	 * JSONObject(); JSONObject jb10 = new JSONObject(); for (GaugeType gt :
	 * gaugeTypeList) { jb1.put(gt.getGaugeName(), gt.getAbbreviation()); } for
	 * (Department dt : departmentList) { jb2.put(dt.getFactory() + "-" +
	 * dt.getDepartmentName(), dt.getDepartmentNum()); } //for (String str :
	 * gaugeSpecification) { // jb3.putAll(str); //} ja.add(jb1); ja.add(jb2); }
	 */
	@RequestMapping("getName.do")

	public ModelAndView getName() {
		List<Map<String, Object>> nameList = service.getName();
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/getName.html");
		mv.addObject("nameList", nameList);
		return mv;

	}

	@RequestMapping("getThDropList.do")

	public void getThDropList(HttpServletResponse response, String thName) throws Exception {
		JSONObject jb = new JSONObject();
		if (thName.contentEquals("gaugeName")) {
			jb = service.getGaugeNames();

		} else if (thName.contentEquals("gaugeAccuracy")) {
			jb = service.getGaugeAccuracy();
		} else if (thName.contentEquals("gaugeModel")) {
			jb = service.getGaugeModel();
		} else if (thName.contentEquals("gaugeManufacturer")) {
			jb = service.getGaugeManufacturer();
		} else if (thName.contentEquals("gaugeRemainingDays")) {
			jb = service.getGaugeRemainingDays();
		} else if (thName.contentEquals("gaugeOwner")) {
			jb = service.getGaugeOwner();
		} else if (thName.contentEquals("gaugeCalibratePeriod")) {
			jb = service.getGaugeCalibratePeriod();
		} else if (thName.contentEquals("gaugeFolder")) {
			jb = service.getGaugeFolder();
		} else if (thName.contentEquals("calibrationResult")) {
			jb = service.getCalibrationResult();
		} else if (thName.contentEquals("gaugeStatus")) {
			jb = service.getGaugeStatus();
		} else if (thName.contentEquals("gaugeSpecification")) {
			jb = service.getGaugeSpecification();
		} else if (thName.contentEquals("department")) {
			jb = service.getDepartments();
		}
		try {
			response.getWriter().print(jb);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		;
	}

	@RequestMapping("searchGauge.do")
	@ResponseBody
	public void searchGauge(HttpServletResponse response, Gauge gauge, HttpServletRequest request) {
		int currentPage = Integer.parseInt(request.getParameter("pageNum"));
		JSONArray ja = service.searchGauge(gauge, currentPage);
		try {
			response.getWriter().print(ja);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("removeGauge.do")
	public void removeGauge(HttpServletResponse response, String gaugeIds) {
		String[] sids = gaugeIds.split(",");
		int[] ids = new int[sids.length];
		for (int i = 0; i < sids.length; i++) {
			ids[i] = Integer.parseInt(sids[i]);
		}
		int result = service.removeGauge(ids);
		JSONObject jb = new JSONObject();
		if (result > 0) {
			jb.put("status", "s");
			jb.put("result", result);
		}
		try {
			response.getWriter().print(jb);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("getAllGaugesByPost.do")

	public void getAllGaugesByPost(HttpServletResponse response) {

		JSONArray ja = new JSONArray();
		ja = service.getAllGaugesByPost();
		try {
			response.getWriter().print(ja);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * public ModelAndView getAllGauges() { List<Gauge> gaugeList =
	 * service.getAllGauges(); ModelAndView mv = new ModelAndView();
	 * mv.setViewName("/allGauges.html"); mv.addObject("gaugeList", gaugeList);
	 * 
	 * mv.addObject("gaugeTypeList", gaugeTypeList); mv.addObject("departmentList",
	 * departmentList); mv.addObject("gaugeSpecification", gaugeSpecification);
	 * mv.addObject("gaugeAccuracy", gaugeAccuracy); mv.addObject("gaugeModel",
	 * gaugeModel); mv.addObject("gaugeManufacturer", gaugeManufacturer);
	 * mv.addObject("gaugeRemainingDays", gaugeRemainingDays);
	 * mv.addObject("gaugeOwner", gaugeOwner); mv.addObject("gaugeCalibratePeriod",
	 * gaugeCalibratePeriod); mv.addObject("gaugeFolder", gaugeFolder);
	 * 
	 * return mv;
	 * 
	 * }
	 */
	@RequestMapping("editGauge.do")
	public void editGauge(HttpServletResponse response, Gauge gauge) {
		String[] result = service.editGauge(gauge);
		String info = null;
		JSONObject jb = new JSONObject();
		if (result[0].contentEquals("s")) {
			info = "修改成功!" + result[1];
			jb.put("status", "s");

		} else {
			info = "失败!";
			jb.put("status", "f");
		}
		jb.put("info", info);
		try {
			response.getWriter().print(jb);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("addGauge.do")
	public void addGauge(HttpServletResponse response, Gauge gauge) {
		String result = service.addGauge(gauge);
		String info = null;
		JSONObject jb = new JSONObject();
		if (result.equals("-1")) {
			info = "错误! 已经存在一个相同仪器编号的量具了!";
			jb.put("status", "f");
		} else {
			info = "成功! 新增的量具精英编号为:" + result;
			jb.put("status", "s");

		}
		jb.put("info", info);
		try {
			response.getWriter().print(jb);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("getOverViewInfo.do")
	@ResponseBody
	public void getOverViewInfo(HttpServletResponse response) {
		JSONObject jb = service.getOverViewInfo();

		try {
			response.getWriter().print(jb);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("test")
	public String test(HttpServletRequest request) {
		return "test.html";
	}

}
